#include "my_inc/main_login.h"
#include "my_inc/music.h"

// 音乐界面
void music(face_p head)
{
    face_p temp        = faceinsertend(head, "音乐");
    lv_obj_t * img_mus = lv_img_create(temp->screen);
    lv_img_set_src(img_mus, "A:img/main_face.png");
    lv_obj_set_size(img_mus, 800, 480);

    // 定义网格列和行的尺寸数组
    static lv_coord_t col_dsc[] = {180, 180, 180, 180, LV_GRID_TEMPLATE_LAST}; // col_dsc: 定义4列，每列宽度70像素
    static lv_coord_t row_dsc[] = {180, 180, 65, LV_GRID_TEMPLATE_LAST};       // row_dsc: 定义2行，每行高度50像素
    lv_obj_t * cont             = lv_obj_create(temp->screen);                 // 创建容器对象
    lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE);                           // ⭐️ 关键代码：禁用滚动
    lv_obj_set_style_bg_color(cont, lv_color_hex(0xFFFFFF), LV_PART_MAIN);     // 设置背景色
    lv_obj_set_style_bg_opa(cont, LV_OPA_0, LV_PART_MAIN);                     // 只设置背景的透明度
    lv_obj_set_style_grid_column_dsc_array(cont, col_dsc, 0);                  // 设置列和行的尺寸数组
    lv_obj_set_style_grid_row_dsc_array(cont, row_dsc, 0);                     // 设置列和行的尺寸数组
    lv_obj_set_size(cont, 800, 480);                                           // 设置容器大小(800x440)
    lv_obj_align(cont, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_set_layout(cont, LV_LAYOUT_GRID); // 设置布局为网格布局 LV_LAYOUT_GRID

    lv_obj_t * circle_obj = lv_obj_create(cont);
    lv_obj_set_size(circle_obj, 180, 180); // 宽=高=100px // 1. 设置宽高相等（确保是正方形，才能变成正圆）
    // 说明：LV_RADIUS_CIRCLE 是 LVGL 宏，等价于 "宽高的一半"，自动适配尺寸
    lv_obj_set_style_radius(circle_obj, LV_RADIUS_CIRCLE, LV_PART_MAIN); // 2.
    // 设置圆角半径为宽高的一半（核心：让正方形变成圆形）
    lv_obj_set_style_bg_color(circle_obj, lv_color_hex(0x999999), LV_PART_MAIN);     // 背景
    lv_obj_clear_flag(circle_obj, LV_OBJ_FLAG_SCROLLABLE);                           // ⭐️ 关键代码：禁用滚动
    lv_obj_set_style_bg_opa(circle_obj, LV_OPA_100, LV_PART_MAIN);                   // 不透明
    lv_obj_set_style_border_color(circle_obj, lv_color_hex(0xFFFFFF), LV_PART_MAIN); // 白色边框
    lv_obj_set_style_border_width(circle_obj, 2, LV_PART_MAIN);                      //    边框宽度 2px
    lv_obj_center(circle_obj); // 4. （可选）定位圆形对象（居中显示）
    lv_obj_set_grid_cell(circle_obj, LV_GRID_ALIGN_STRETCH, 1, 2, LV_GRID_ALIGN_STRETCH, 0, 2);

    lv_obj_t * play = lv_obj_create(cont);
    lv_obj_set_style_pad_all(play, 0, LV_PART_MAIN); // 去掉默认的内补 20
    lv_obj_set_grid_cell(play, LV_GRID_ALIGN_STRETCH, 0, 4, LV_GRID_ALIGN_STRETCH, 2, 1);
    lv_obj_set_style_bg_color(cont, lv_color_hex(0xFFFFFF), LV_PART_MAIN); // 设置背景色
    lv_obj_set_style_bg_opa(play, LV_OPA_30, LV_PART_MAIN);                // 只设置背景的透明度

    lv_obj_t * btn_play = lv_btn_create(play);
    lv_obj_center(btn_play);
    lv_obj_set_style_bg_opa(btn_play, LV_OPA_0, LV_PART_MAIN); // 只设置背景的透明度
    lv_obj_t * label_play = lv_label_create(btn_play);
    lv_label_set_text(label_play, LV_SYMBOL_PLAY);
    lv_obj_set_style_text_font(label_play, &lv_font_montserrat_48, LV_PART_MAIN);
    lv_obj_center(label_play);

    lv_obj_t * btn_pause = lv_btn_create(play);
    lv_obj_add_flag(btn_pause, LV_OBJ_FLAG_HIDDEN); // 隐藏OBJ
    lv_obj_center(btn_pause);
    lv_obj_set_style_bg_opa(btn_pause, LV_OPA_0, LV_PART_MAIN); // 只设置背景的透明度
    lv_obj_t * label_pause = lv_label_create(btn_pause);
    lv_label_set_text(label_pause, LV_SYMBOL_PAUSE);
    lv_obj_set_style_text_font(label_pause, &lv_font_montserrat_48, LV_PART_MAIN);
    lv_obj_center(label_pause);

    lv_obj_t * btn_prev = lv_btn_create(play);
    lv_obj_align(btn_prev, LV_ALIGN_LEFT_MID, 100, 0);
    lv_obj_set_style_bg_opa(btn_prev, LV_OPA_0, LV_PART_MAIN); // 只设置背景的透明度
    lv_obj_t * label_prev = lv_label_create(btn_prev);
    lv_label_set_text(label_prev, LV_SYMBOL_PREV);
    lv_obj_set_style_text_font(label_prev, &lv_font_montserrat_30, LV_PART_MAIN);
    lv_obj_center(label_prev);

    lv_obj_t * btn_next = lv_btn_create(play);
    lv_obj_align(btn_next, LV_ALIGN_RIGHT_MID, -100, 0);
    lv_obj_set_style_bg_opa(btn_next, LV_OPA_0, LV_PART_MAIN); // 只设置背景的透明度
    lv_obj_t * label_next = lv_label_create(btn_next);
    lv_label_set_text(label_next, LV_SYMBOL_NEXT);
    lv_obj_set_style_text_font(label_next, &lv_font_montserrat_30, LV_PART_MAIN);
    lv_obj_center(label_next);

    lv_obj_t * obj = lv_obj_create(cont);
    lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);       // 隐藏OBJ
    lv_obj_set_style_pad_all(obj, 0, LV_PART_MAIN); // 去掉默认的内补 20
    lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0, 4, LV_GRID_ALIGN_STRETCH, 2, 1);
    lv_obj_set_style_bg_color(cont, lv_color_hex(0xFFFFFF), LV_PART_MAIN); // 设置背景色
    lv_obj_set_style_bg_opa(obj, LV_OPA_30, LV_PART_MAIN);                 // 只设置背景的透明度

    lv_obj_t * btn_home = lv_btn_create(obj);
    lv_obj_center(btn_home);
    lv_obj_t * home = lv_label_create(btn_home);
    lv_label_set_text(home, LV_SYMBOL_HOME);

    lv_obj_t * btn_close = lv_btn_create(obj);
    lv_obj_align(btn_close, LV_ALIGN_RIGHT_MID, -100, 0);
    lv_obj_t * close = lv_label_create(btn_close);
    lv_label_set_text(close, LV_SYMBOL_POWER);

    lv_obj_t * btn_return = lv_btn_create(obj);
    lv_obj_align(btn_return, LV_ALIGN_LEFT_MID, 100, 0);
    lv_obj_t * label_return = lv_label_create(btn_return);
    lv_label_set_text(label_return, LV_SYMBOL_LEFT);
    // lv_obj_add_event_cb(btn_return, face_return, LV_EVENT_CLICKED, head); // 添加登录按钮事件

    // 提醒界面的文本
    lv_obj_t * label_w = lv_label_create(temp->screen);
    lv_label_set_text(label_w, "多远都要在一起");
    lv_obj_align_to(label_w, btn_next, LV_ALIGN_BOTTOM_MID, -70, -70);
    lv_obj_set_style_text_color(label_w, lv_color_hex(0xffffff), LV_PART_MAIN);
    lv_obj_set_style_text_font(label_w, &SourceHanSansCN_Bold_20, LV_PART_MAIN);

    music_p m_btn_p     = malloc(sizeof(music_t));
    m_btn_p->head       = head;
    m_btn_p->circle_obj = circle_obj;
    m_btn_p->btn_play   = btn_play;
    m_btn_p->label_w    = label_w;
    m_btn_p->btn_pause  = btn_pause;
    m_btn_p->btn_prev   = btn_prev;
    m_btn_p->btn_next   = btn_next;
    m_btn_p->btn_home   = btn_home;
    m_btn_p->btn_return = btn_return;
    m_btn_p->play_obj   = play;
    m_btn_p->obj        = obj;
    m_btn_p->sign_obj   = 0;
    m_btn_p->p_sign     = 0;
    m_btn_p->mp3        = 1;

    // 第一次进入时创建进度条
    create_progress_ui(m_btn_p);

    create_rotating_image(m_btn_p, "A:img/music/c_dzq.png");
    set_rotation_speed(m_btn_p, 200);

    // 菜单按钮
    lv_obj_t * btn_menu = lv_btn_create(temp->screen);
    lv_obj_align(btn_menu, LV_ALIGN_TOP_RIGHT, -20, 20);
    lv_obj_t * label_menu = lv_label_create(btn_menu);
    lv_label_set_text(label_menu, LV_SYMBOL_LIST);
    lv_obj_add_event_cb(btn_menu, menu_music, LV_EVENT_CLICKED, m_btn_p);

    lv_obj_add_event_cb(btn_play, menu_play, LV_EVENT_CLICKED, m_btn_p);   // 播放按钮
    lv_obj_add_event_cb(btn_pause, menu_pause, LV_EVENT_CLICKED, m_btn_p); // 暂停按钮
    lv_obj_add_event_cb(btn_prev, menu_prev, LV_EVENT_CLICKED, m_btn_p);   // 上一首按钮事件
    lv_obj_add_event_cb(btn_next, menu_next, LV_EVENT_CLICKED, m_btn_p);   // 下一首按钮事件
    lv_obj_add_event_cb(btn_home, menu_home, LV_EVENT_CLICKED, m_btn_p);   // home按钮事件
    lv_obj_add_event_cb(btn_return, menu_ret, LV_EVENT_CLICKED, m_btn_p);  // 退出按钮事件
}